from Utils import *


def remain1(data):
    table = [[0, 6, 0, 1], [1, 4, 1, 0], [2, 5, 0, 1], [3, 0, 0, 1], [4, 2, 0, 1], [5, 3, 0, 1], [6, 1, 1, 0],
             [7, 7, 0, 1]]
    arr = toBit(data)
    size = len(arr)
    arr1 = [0 for i in range(size)]
    for i in range(size):
        if arr[i] == 0:
            arr1[table[i][1]] = table[i][2]
        else:
            arr1[table[i][1]] = table[i][3]
    return toBytes(arr1)


def remain2(data):
    table = [[0, 5, 0, 1], [1, 9, 0, 1], [2, 0, 1, 0], [3, 7, 1, 0], [4, 10, 0, 1], [5, 6, 0, 1], [6, 13, 1, 0],
             [7, 1, 0, 1],
             [8, 4, 0, 1], [9, 11, 0, 1], [10, 14, 1, 0], [11, 3, 1, 0], [12, 12, 0, 1], [13, 15, 1, 0], [14, 8, 0, 1],
             [15, 2, 0, 1]]
    arr = toBit(data)
    size = len(arr)
    arr1 = [0 for i in range(size)]
    for i in range(size):
        if arr[i] == 0:
            arr1[table[i][1]] = table[i][2]
        else:
            arr1[table[i][1]] = table[i][3]
    return toBytes(arr1)


def remain3(data):
    table = [[0, 17, 0, 1], [1, 7, 0, 1], [2, 5, 0, 1], [3, 19, 1, 0], [4, 18, 0, 1], [5, 15, 1, 0], [6, 22, 0, 1],
             [7, 21, 0, 1],
             [8, 16, 0, 1], [9, 4, 0, 1], [10, 12, 0, 1], [11, 2, 1, 0], [12, 10, 1, 0], [13, 13, 1, 0], [14, 20, 1, 0],
             [15, 8, 1, 0],
             [16, 9, 0, 1], [17, 23, 0, 1], [18, 11, 1, 0], [19, 6, 0, 1], [20, 1, 0, 1], [21, 3, 1, 0], [22, 0, 1, 0],
             [23, 14, 0, 1]]
    arr = toBit(data)
    size = len(arr)
    arr1 = [0 for i in range(size)]
    for i in range(size):
        if arr[i] == 0:
            arr1[table[i][1]] = table[i][2]
        else:
            arr1[table[i][1]] = table[i][3]
    return toBytes(arr1)


def remain4(data):
    table = [[0, 25, 1, 0], [1, 4, 0, 1], [2, 29, 0, 1], [3, 1, 0, 1], [4, 27, 1, 0], [5, 18, 1, 0], [6, 23, 1, 0],
             [7, 14, 1, 0],
             [8, 28, 1, 0], [9, 11, 0, 1], [10, 9, 1, 0], [11, 13, 0, 1], [12, 24, 1, 0], [13, 0, 1, 0], [14, 5, 0, 1],
             [15, 2, 1, 0],
             [16, 26, 0, 1], [17, 12, 0, 1], [18, 31, 1, 0], [19, 16, 1, 0], [20, 30, 0, 1], [21, 15, 0, 1],
             [22, 10, 0, 1], [23, 22, 1, 0],
             [24, 7, 1, 0], [25, 21, 0, 1], [26, 6, 1, 0], [27, 3, 1, 0], [28, 8, 1, 0], [29, 20, 0, 1], [30, 19, 1, 0],
             [31, 17, 0, 1]]
    arr = toBit(data)
    size = len(arr)
    arr1 = [0 for i in range(size)]
    for i in range(size):
        if arr[i] == 0:
            arr1[table[i][1]] = table[i][2]
        else:
            arr1[table[i][1]] = table[i][3]
    return toBytes(arr1)


def remain5(data):
    table = [[0, 11, 0, 1], [1, 12, 0, 1], [2, 28, 1, 0], [3, 30, 0, 1], [4, 13, 1, 0], [5, 24, 0, 1], [6, 22, 1, 0],
             [7, 25, 1, 0],
             [8, 23, 1, 0], [9, 3, 0, 1], [10, 16, 0, 1], [11, 8, 1, 0], [12, 34, 0, 1], [13, 2, 0, 1], [14, 5, 0, 1],
             [15, 7, 1, 0],
             [16, 4, 0, 1], [17, 14, 0, 1], [18, 39, 1, 0], [19, 33, 0, 1], [20, 15, 0, 1], [21, 0, 0, 1],
             [22, 31, 0, 1], [23, 9, 1, 0],
             [24, 29, 0, 1], [25, 26, 1, 0], [26, 19, 0, 1], [27, 6, 1, 0], [28, 27, 1, 0], [29, 10, 1, 0],
             [30, 37, 0, 1], [31, 38, 1, 0],
             [32, 20, 0, 1], [33, 21, 1, 0], [34, 1, 0, 1], [35, 36, 0, 1], [36, 32, 0, 1], [37, 17, 0, 1],
             [38, 18, 0, 1], [39, 35, 1, 0]]
    arr = toBit(data)
    size = len(arr)
    arr1 = [0 for i in range(size)]
    for i in range(size):
        if arr[i] == 0:
            arr1[table[i][1]] = table[i][2]
        else:
            arr1[table[i][1]] = table[i][3]
    return toBytes(arr1)


def remain6(data):
    table = [[0, 11, 0, 1], [1, 45, 0, 1], [2, 15, 1, 0], [3, 22, 0, 1], [4, 10, 0, 1], [5, 7, 0, 1], [6, 3, 0, 1],
             [7, 42, 0, 1],
             [8, 17, 1, 0], [9, 21, 0, 1], [10, 4, 0, 1], [11, 8, 1, 0], [12, 19, 1, 0], [13, 32, 0, 1], [14, 28, 1, 0],
             [15, 31, 1, 0],
             [16, 29, 0, 1], [17, 14, 1, 0], [18, 39, 1, 0], [19, 27, 1, 0], [20, 2, 1, 0], [21, 24, 0, 1],
             [22, 26, 1, 0], [23, 9, 1, 0],
             [24, 41, 0, 1], [25, 1, 1, 0], [26, 47, 0, 1], [27, 44, 0, 1], [28, 23, 1, 0], [29, 0, 1, 0],
             [30, 12, 1, 0], [31, 18, 0, 1],
             [32, 33, 0, 1], [33, 36, 0, 1], [34, 40, 1, 0], [35, 34, 0, 1], [36, 25, 0, 1], [37, 16, 1, 0],
             [38, 5, 1, 0], [39, 35, 0, 1],
             [40, 38, 0, 1], [41, 37, 1, 0], [42, 13, 0, 1], [43, 20, 1, 0], [44, 6, 0, 1], [45, 43, 0, 1],
             [46, 30, 0, 1], [47, 46, 1, 0]]
    arr = toBit(data)
    size = len(arr)
    arr1 = [0 for i in range(size)]
    for i in range(size):
        if arr[i] == 0:
            arr1[table[i][1]] = table[i][2]
        else:
            arr1[table[i][1]] = table[i][3]
    return toBytes(arr1)


def remain7(data):
    table = [[0, 7, 1, 0], [1, 9, 0, 1], [2, 53, 1, 0], [3, 19, 1, 0], [4, 15, 1, 0], [5, 8, 0, 1], [6, 3, 0, 1],
             [7, 24, 1, 0],
             [8, 18, 0, 1], [9, 51, 0, 1], [10, 42, 1, 0], [11, 39, 0, 1], [12, 20, 0, 1], [13, 12, 0, 1],
             [14, 28, 1, 0], [15, 27, 1, 0],
             [16, 23, 0, 1], [17, 49, 0, 1], [18, 10, 1, 0], [19, 55, 1, 0], [20, 52, 1, 0], [21, 17, 0, 1],
             [22, 48, 0, 1], [23, 14, 1, 0],
             [24, 33, 0, 1], [25, 25, 1, 0], [26, 4, 1, 0], [27, 11, 0, 1], [28, 47, 1, 0], [29, 0, 0, 1],
             [30, 21, 1, 0], [31, 44, 0, 1],
             [32, 16, 0, 1], [33, 41, 0, 1], [34, 29, 0, 1], [35, 1, 0, 1], [36, 46, 0, 1], [37, 5, 0, 1],
             [38, 30, 0, 1], [39, 45, 0, 1],
             [40, 31, 1, 0], [41, 43, 1, 0], [42, 36, 1, 0], [43, 26, 0, 1], [44, 34, 0, 1], [45, 2, 0, 1],
             [46, 6, 0, 1], [47, 50, 1, 0],
             [48, 13, 1, 0], [49, 37, 1, 0], [50, 32, 0, 1], [51, 40, 0, 1], [52, 35, 0, 1], [53, 38, 0, 1],
             [54, 54, 0, 1], [55, 22, 0, 1]]
    arr = toBit(data)
    size = len(arr)
    arr1 = [0 for i in range(size)]
    for i in range(size):
        if arr[i] == 0:
            arr1[table[i][1]] = table[i][2]
        else:
            arr1[table[i][1]] = table[i][3]
    return toBytes(arr1)


def TenMagic32_enc(input):
    table = [[0, 0], [1, 4], [2, 61], [3, 15], [4, 56], [5, 40], [6, 6], [7, 59], [8, 62], [9, 58], [10, 17], [11, 2],
             [12, 12], [13, 8], [14, 32], [15, 60], [16, 13], [17, 45], [18, 34], [19, 14], [20, 36], [21, 21],
             [22, 22], [23, 39], [24, 23], [25, 25], [26, 26], [27, 20], [28, 1], [29, 33], [30, 46], [31, 55],
             [32, 35], [33, 24], [34, 57], [35, 19], [36, 53], [37, 37], [38, 38], [39, 5], [40, 30], [41, 41],
             [42, 42], [43, 18], [44, 47], [45, 27], [46, 9], [47, 44], [48, 51], [49, 7], [50, 49], [51, 63], [52, 28],
             [53, 43], [54, 54], [55, 52], [56, 31], [57, 10], [58, 29], [59, 11], [60, 3], [61, 16], [62, 50],
             [63, 48]]
    arr = input
    arr1 = [0 for i in range(len(arr))]
    for i in range(len(table)):
        arr1[table[i][1]] = arr[table[i][0]]
    return toBytes(arr1)


def TenMagic32(data, data_len):
    key = "44e715a6e322ccb7d028f7a42fa55040"
    split_num = data_len // 8
    remain = data_len % 8
    result = b""
    for i in range(split_num):
        data_bit = toBit(data[8 * i: 8 * i + 8])
        result += TenMagic32_enc(data_bit)
    switcher = {
        1: remain1,
        2: remain2,
        3: remain3,
        4: remain4,
        5: remain5,
        6: remain6,
        7: remain7
    }
    if remain != 0:
        result += switcher[remain](data[split_num * 8:])

    return result
